package com.zb.nio.channel;

import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;

/**
 * @author: zhaobin
 * @date: 2023/12/23 14:38
 * @Description: 通道间数据传输
 */
public class FileChannelDemo3 {
    public static void main(String[] args) throws Exception {
        // 创建通道1
        RandomAccessFile fromFile = new RandomAccessFile("from.txt", "rw");
        FileChannel fromChannel = fromFile.getChannel();
        // 创建通道2
        RandomAccessFile toFile = new RandomAccessFile("to.txt", "rw");
        FileChannel toChannel = toFile.getChannel();

        // 传输
        long position = 0L;
        long size = fromChannel.size();
        //toChannel.transferFrom(fromChannel, position, size);
        fromChannel.transferTo(position,size,toChannel);

        // 关闭通道
        fromChannel.close();
        toChannel.close();
        System.out.println("over!");
    }
}
